package me.numark.activities;

import android.app.Dialog;
import android.content.DialogInterface;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.kyleduo.switchbutton.SwitchButton;
import com.sina.weibo.sdk.utils.LogUtil;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import me.numark.R;
import me.numark.model.LogInResponse;
import me.numark.model.User;
import me.numark.net.BaseResponse;
import me.numark.net.NetworkConstants;
import me.numark.net.RequestServerTask;
import me.numark.utils.Global;
import me.numark.utils.HttpUtil;
import me.numark.utils.LogUtils;
import me.numark.utils.Settings;
import me.numark.utils.Utils;

public class MsgRemindActivity extends BaseActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {

    @Bind(R.id.back)
    View back;

    @Bind(R.id.switch_received_labels)
    SwitchButton switchLabels;

    @Bind(R.id.switch_received_msg)
    SwitchButton switchMsg;

    @Bind(R.id.time_span)
    FrameLayout timeSpan;

    @Bind(R.id.time)
    TextView time;

    private User mUser;
    private int mtime;
    private Dialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        activity_layout_res = R.layout.activity_msg_remind;
        super.onCreate(savedInstanceState);
        uiInit();
    }

    private void uiInit() {
        titleTextView.setText("消息提醒");
        int i = Settings.getMsgInterval();
        updateTime(i);
        switchLabels.setChecked(Settings.getSwitchLabels());
        switchMsg.setChecked(Settings.getSwitchMsg());
        timeSpan.setOnClickListener(this);
        back.setOnClickListener(this);
        switchLabels.setOnCheckedChangeListener(this);
        switchMsg.setOnCheckedChangeListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back:
                onBackPressed();
                break;
            case R.id.time_span:
                showTimeDialog();
                break;
            case R.id.thirty_minutes:
                mtime = 30;
                setTime(mtime);
                break;
            case R.id.every_time:
                mtime = 1;
                setTime(mtime);
                dialog.dismiss();
                break;
            case R.id.one_hour:
                mtime = 60;
                setTime(mtime);
                dialog.dismiss();
                break;
            case R.id.two_hour:
                mtime = 120;
                setTime(mtime);
                dialog.dismiss();
                break;
            case R.id.five_hour:
                mtime = 300;
                setTime(mtime);
                dialog.dismiss();
                break;
            case R.id.one_day:
                mtime = 1440;
                setTime(mtime);
                dialog.dismiss();
                break;
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()){
            case R.id.switch_received_labels:
                setMessageReminder();
                break;
            case R.id.switch_received_msg:
                setMessageReminder();
                break;
            default:
                break;
        }
    }

    private void setMessageReminder() {

        new RequestServerTask<LogInResponse>(LogInResponse.class) {
            @Override
            protected String requestServer() {
                Map<String,Object> map = new HashMap<String, Object>();
                map.put("is_label", Boolean.toString(switchLabels.isChecked()));
                //mUser.is_topic = switchComments.isChecked();
                map.put("is_dialog", Boolean.toString(switchMsg.isChecked()));
                //mUser.is_like = switchLikes.isChecked();
                String url = String.format(NetworkConstants.AUTH_MESSAGE_REMIND, Global.getCurrentUid());
                return HttpUtil.post(url,map);
            }

            @Override
            protected void onSuccess(LogInResponse result) {
                Settings.setSwitchLabels(result.data.is_label);
                Settings.setSwitchMsg(result.data.is_dialog);
                Utils.showSnackbar(rootView,R.string.commit_succ);
            }
        }.start();
    }

    void showTimeDialog() {
       /* final AlertDialog.Builder ab = new AlertDialog.Builder(this);
        final String[] items = new String[]{getString(R.string.interval_1),
                getString(R.string.interval_5),
                getString(R.string.interval_60)};
        ab.setTitle("设置提醒时间");
        ab.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (which) {
                    case 0:
                        mtime = 1;
                        break;
                    case 1:
                        mtime = 5;
                        break;
                    case 2:
                        mtime = 60;
                        break;
                }
                time.setText(items[which]);
                setTime(mtime);
            }
        });
        ab.setNegativeButton(android.R.string.cancel, null);
        ab.show();*/
        View view = getLayoutInflater().inflate(R.layout.choose_time, null);
        Button everyTime = (Button)view.findViewById(R.id.every_time);
        Button thirtyMinutes = (Button) view.findViewById(R.id.thirty_minutes);
        Button oneHour = (Button)view.findViewById(R.id.one_hour);
        Button twoHour = (Button)view.findViewById(R.id.two_hour);
        Button fiveHour = (Button)view.findViewById(R.id.five_hour);
        Button oneday = (Button)view.findViewById(R.id.one_day);

        everyTime.setOnClickListener(this);
        thirtyMinutes.setOnClickListener(this);
        oneHour.setOnClickListener(this);
        twoHour.setOnClickListener(this);
        fiveHour.setOnClickListener(this);
        oneday.setOnClickListener(this);

        dialog = new Dialog(this, R.style.transparentFrameWindowStyle);
        dialog.setContentView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
        Window window = dialog.getWindow();

        window.setWindowAnimations(R.style.menu_animstyle);
        WindowManager.LayoutParams wl = window.getAttributes();
        wl.x = 0;
        wl.y = getWindowManager().getDefaultDisplay().getHeight();

        wl.width = ViewGroup.LayoutParams.MATCH_PARENT;
        wl.height = ViewGroup.LayoutParams.WRAP_CONTENT;

        dialog.onWindowAttributesChanged(wl);
        dialog.setCanceledOnTouchOutside(true);
        dialog.show();
    }

    private void setTime(final int mtime) {
        new RequestServerTask<LogInResponse>(LogInResponse.class) {
            @Override
            protected String requestServer() {
                Map<String,Object> map = new HashMap<String, Object>();
                map.put("interval", mtime);
                String url = String.format(NetworkConstants.MESSAGE_INTERVAL_URL, Global.getCurrentUid());
                return HttpUtil.post(url,map);
            }

            @Override
            protected void onSuccess(LogInResponse result) {
                updateTime(mtime);
                Settings.setMsgInterval(Integer.parseInt(result.data.message_interval));
                Utils.showSnackbar(rootView,R.string.commit_succ);
            }
        }.start();
    }


    private void updateTime(int i){
        switch (i){
            case 1:
                time.setText(getString(R.string.every_time));
                break;
            case 30:
                time.setText(getString(R.string.thirty_minutes));
                break;
            case 60:
                time.setText(getString(R.string.interval_60));
                break;
            case 120:
                time.setText(getString(R.string.two_hour));
                break;
            case 300:
                time.setText(getString(R.string.five_hour));
                break;
            case 1440:
                time.setText(getString(R.string.one_day));
                break;
        }
    }

}
